package edu.msu.cse.meldru20.project1;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;


@SuppressLint("ShowToast")
public class MainActivity extends Activity {

    /**
     * The Start button
     */
    private Button startGame = null;

    /**
     * The First Player
     */
    
    private EditText player1 = null;
    /**
     * The Second Player
     */
    private EditText player2 = null;
    
    final Context context = this;
    
    private GameManager game = null;
    
    private Toast toastRounds = null;
    
    private SeekBar seekRounds = null;
    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		game = GameManager.get();
		game.Init();

		startGame = (Button)findViewById(R.id.StartButton);
		player1 = (EditText)findViewById(R.id.Player1);
		player2 = (EditText)findViewById(R.id.Player2);
		
		startGame.setEnabled(false);
		
		player1.requestFocus();
		
		//player1.setText(game.getPlayer1());
		//player2.setText(game.getPlayer2());
		
		player1.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable s) {
				updateUI();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		player2.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable s) {
				updateUI();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		toastRounds = Toast.makeText(this, "", Toast.LENGTH_SHORT);
		toastRounds.setGravity(Gravity.CENTER, 0, 0);
		
		seekRounds = (SeekBar) findViewById(R.id.seekBarRounds);
		seekRounds.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if(progress > 0) {
					game.setNumOfRounds(progress);
				}
				else {
					seekBar.setProgress(1);
				}
				toastRounds.setText("Number of rounds:" + game.getNumOfRounds());
				toastRounds.show();
				updateUI();
			}
		});
	}
	
	public void onHowToPlay(View view){
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		alertDialogBuilder.setTitle("How To Play");
		alertDialogBuilder.setMessage("Each player takes turns drawing pictures into " +
				"the box that are affiliated with the category given.  They give a hint " +
				"that will be shown to the other player and the answer then submit, giving " +
				"the second player a chance to guess.  If after 1 minute the second player " +
				"can't guess then they will be shown the hint.  Turns alternate in this way, " +
				"giving more points for faster answers.");
		//alertDialogBuilder.setCancelable(false);
		alertDialogBuilder.setPositiveButton("Okay", null);
		AlertDialog alertDialog = alertDialogBuilder.create();
		alertDialog.show();
	}
	
	public void onClear(View view){
		player1.setText("");
		player2.setText("");
	}
	
    public void onStartGame(View view){
    	game.setPlayer1(player1.getText().toString().trim());
    	game.setPlayer2(player2.getText().toString().trim());
    	
    	Intent intent = new Intent(this, EditingActivity.class);
    	startActivity(intent);
    }
    
    /**
     * Ensure the user interface components match the current state
     */
    private void updateUI() {
    	//
    	//
    	// Fix this -Mike
    	//
    	if (player1.getText().toString().trim().equals("") || player2.getText().toString().trim().equals("")
    			|| player1.getText().toString().trim().equals(player2.getText().toString().trim())) {
    		// We need both of the players to have names before we start the game
    		startGame.setEnabled(false);
    	}
    	else {
    		startGame.setEnabled(true);
    	}
    	
    	seekRounds.setProgress(game.getNumOfRounds());
    }
	

}
